import React, { PureComponent } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import Button from '../../common/Button';
import UserTool from './UserTool';
import TransferList from './TransferList';
import pan_logo from '../../../asserts/logo@2x.png';
import './index.less';
import { toggleTransfer } from '../../../redux/actions/transferVisible';

class Header extends PureComponent {

  static propTypes = {
    user: PropTypes.object,
    toggleTransfer: PropTypes.func
  };

  state = {
    toolbarVisible: false
  };

  // 显示toolbar
  showToolbar = () => {
    clearTimeout(this.timerId);
    this.setState({ toolbarVisible: true });
  };

  // 隐藏toolbar
  hideToolbar = () => {
    this.timerId = setTimeout(() => {
      this.setState({ toolbarVisible: false });
    }, 60);
  };

  // 初始化vip信息
  initVipInfo = () => {
    const { user } = this.props;
    let isVip = false;
    let now = new Date().getTime()
    let timeout = new Date(user.vipTimeout).getTime()
    if (user.vipState === 2 && timeout > now) {
      isVip = true
    }
    return isVip
  }

  // 切换传输列表显示
  toggleTransferList = (event) => {
    const { toggleTransfer } = this.props
    event.stopPropagation()
    toggleTransfer()
  }


  render () {
    const { user } = this.props;
    let isVip = this.initVipInfo()
    return (
      <>
        <div className="pan_header">
          <div className="pan_logo">
            <img src={pan_logo} alt=""/>
          </div>
          <ul className="pan_nav">
            <li className="pan_nav_item">
              <a>
                限时低价！激活超级会员
              </a>
            </li>
            <li className="pan_nav_item">
              <a>
                企业认证
              </a>
            </li>
            <li
              className={'pan_nav_item ' + (isVip? 'vip': '')}
              onMouseEnter={this.showToolbar}
              onMouseLeave={this.hideToolbar}
            >
              <img src={user.pic} alt=""/>
              <span className="username">
                {
                  user.username
                }
              </span>
              <a/>
              <UserTool visible={this.state.toolbarVisible}/>
            </li>
            <li className="pan_nav_item">
              <a>
                <Button style={{
                  background: 'linear-gradient( 45deg ,#f5deb7, #e9b866)',
                  color: '#64360d',
                }}
                >
                  会员中心
                </Button>
              </a>
            </li>
            <li className="pan_nav_item">
              <a>
                <Button>回到旧版</Button>
              </a>
            </li>
            <li onClick={this.toggleTransferList} className="pan_nav_item"  style={{ position: 'relative' }}>
              <a className="iconfont icon-uploader-trans">
              </a>
              <TransferList visible={this.state.transferListVisible}/>
            </li>
            <li className="pan_nav_item">
              <a className="iconfont icon-message"/>
            </li>
            <li className="pan_nav_item">
              <a className="iconfont icon-feedback"/>
            </li>
            <li className="pan_nav_item">
              <a href="/#">
                客户端下载
              </a>
            </li>
          </ul>
        </div>
      </>
    );
  }
}

export default connect(
  (state) => ({
    user: state.user,
  }),
  {
    toggleTransfer
  }
)(Header);
